package test;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class orderPage extends JFrame {
    private JTable table;

    public orderPage() {
        setTitle("用户信息页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("航班id");
        model.addColumn("航班数量");
        model.addColumn("航线id");
        model.addColumn("出发时间");
        model.addColumn("到达时间");
        model.addColumn("飞机id");
        model.addColumn("航班价格");
        model.addColumn("可预购座位");

        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM air_flight");

            while (resultSet.next()) {
                int flightId = resultSet.getInt("flight_id");
                int flightNum = resultSet.getInt("flight_num");
                int airwayId = resultSet.getInt("airway_id");
                int aircraftId = resultSet.getInt("aircraft_id");

                String departureTime = resultSet.getString("departure_time");
                String arrivalTime = resultSet.getString("arrival_time");
                String price = resultSet.getString("price");
                String availableSeat = resultSet.getString("available_seat");

                Object[] rowData = {flightId,flightNum,airwayId,aircraftId,departureTime,arrivalTime,price,availableSeat};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        add(Panel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            orderPage userInfoView = new orderPage();
            userInfoView.setSize(400, 300);
            userInfoView.setVisible(true);
        });
    }
}